package com.lanyun.web.bean;

import lombok.Data;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 分页返回的实体
 *
 * @Author Jqcc
 * @Version 1.0
 * @Data 2024/2/14
 */
@Data
public class PageResult<T> implements Serializable {

    private Integer page = 1;

    private Integer pageSize = 10;

    private Integer total = 0;

    private Integer totalPage = 0;

    private List<T> result = Collections.emptyList();

    private Integer start = 1;

    private Integer end = 0;

    public void setRecords(List<T> result) {
        this.result = result;
        if (result != null && result.size() > 0) {
            setTotal(result.size());
        }
    }

    public void setTotal(Integer total) {
        this.total = total;
        if (this.pageSize > 0) {
            this.totalPage = (total / this.pageSize) + (total % this.pageSize == 0 ? 1 : 0);
        } else {
            this.pageSize = 0;
        }
        this.start = (this.pageSize > 0 ? (this.page - 1) * this.pageSize : 0) + 1;
        this.end = (this.start - 1 + this.pageSize * (this.page > 0 ? 1 : 0));
    }

}
